package com.smarth.zhizulian.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.utils.KumaToast;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import org.simple.eventbus.EventBus;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by 18865 on 2018/2/1.
 */

public class BottomTabProvider implements SmartTabLayout.TabProvider {
    private Context mContext;

    public BottomTabProvider(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public View createTabView(ViewGroup container, final int position, PagerAdapter adapter) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tab, container, false);
        RelativeLayout ll_main = view.findViewById(R.id.ll_main);
        ImageView iv_icon = view.findViewById(R.id.iv_icon);
        TextView tv_name = view.findViewById(R.id.tv_name);
        LinearLayout ll_refresh = view.findViewById(R.id.ll_refresh);
        GifImageView iv_refresh = view.findViewById(R.id.iv_refresh);
        BottomPagerAdapter pba = (BottomPagerAdapter) adapter;
        iv_icon.setImageResource(pba.getIconList().get(position));
        tv_name.setText(pba.getTitleList().get(position));
        final GifDrawable drawable = (GifDrawable) iv_refresh.getDrawable();
        drawable.stop();
        tv_name.setText(pba.getTitleList().get(position));
        iv_icon.setImageResource(pba.getIconList().get(position));
        ll_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawable.reset();
                drawable.start();
                switch (position) {
                    case 0:
                        EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.REFRESH, "BottomTabProvider"), "CategoryFragment");
                        break;
                    case 1:
                        EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.REFRESH, "BottomTabProvider"), "VideoCategoryFragment");
                        break;
                }
            }
        });
        return view;
    }
}
